package org.darkforest.admin.controller;

import org.darkforest.admin.vo.StatInfoVo;
import org.darkforest.common.services.StatService;
import org.darkforest.common.util.BeanUtils;
import org.darkforest.dao.mapper.AdminMapper;
import org.darkforest.dao.mapper.GAdminMapper;
import org.darkforest.dao.mapper.MenusMapper;
import org.darkforest.model.GAdmin;
import org.darkforest.model.Menus;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by valentina on 15/5/11.
 * 主菜单：控制台中相关操作类
 */
@Controller
@RequestMapping("dashboard")
public class DashBoardController {

    @Autowired
    private StatService statService;
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private GAdminMapper gAdminMapper;

    @Autowired
    private MenusMapper menusMapper;

    /**
     * 登录后首页
     * @return
     */
    @AuthPassport
    @RequestMapping("main")
    public ModelAndView main(HttpSession session,String reUrl) throws Exception {
        MemberBase memberBase = (MemberBase)session.getAttribute("memInfo");
        StatInfoVo statInfo = (StatInfoVo)session.getAttribute("statInfo");
        if(statInfo==null){
            //查询统计信息
            statInfo = new StatInfoVo();
            statInfo.setGoodsAmount(statService.getGoodsCount(null));
            statInfo.setGoodsNew(statService.getGoodsCount(7 * 24 * 3600));
            statInfo.setMemberAmount(statService.getMemberCount(null));
            statInfo.setMemberNew(statService.getMemberCount(7 * 24 * 3600));
            statInfo.setOrderAmount(statService.getOrderCount(null));
            statInfo.setProxyAmount(statService.getProxyCount(null,null));
            statInfo.setProxyNew(statService.getProxyCount(null,0));
            statInfo.setStoreAmount(statService.getStoreCount(null));
            statInfo.setStoreNew(statService.getStoreJoinCount(null));
        }else{
            statInfo.setStoreNew(statService.getStoreJoinCount(null));
//            statInfo.setProxyNew(statService.getProxyCount(null,0));            //对于操作过的数据进行更新
        }
        session.setAttribute("statInfo",statInfo);

        ModelAndView res = new ModelAndView(reUrl==null||reUrl.equals("")?"main":"redirect:"+reUrl);

        // 登录用户权限
        session.setAttribute("loginGAdmin", gAdminMapper.selectByPrimaryKey(memberBase.getAdminGid()));

        // 菜单
        session.setAttribute("loginMenuList", menusMapper.selectByCondition(new HashMap()));

        // res.addObject("statInfo",statInfo);
        return res;
    }

    /**
     * 站点设置,转到站点设置请求
     * @return
     */
    @RequestMapping("setting")
    public String setting(){

        return "redirect:/setting/site";
    }
}
